/*************************************************************************
    > File Name    : Condition.cc
    > Author       : LiuLei
    > Mail         : 2261459486@qq.com 
    > Created Time : 2022年11月24日 星期四 20时34分56秒
 ************************************************************************/

#include "../../include/server/Condition.h"
#include <stdio.h>

Condition::Condition(MutexLock &mutex)
: _cond()
, _mutex(mutex)
{
    int ret = ::pthread_cond_init(&_cond, nullptr);
    if(ret)
    {
        ::perror("pthread_cond_init in Condition::Condition()");
        return ;
    }
}

Condition::~Condition()
{
    int ret = ::pthread_cond_destroy(&_cond);
    if(ret)
    {
        ::perror("pthread_cond_destroy in Condition::~Condition()");
        return ;
    }
}

void Condition::wait()
{
    int ret = ::pthread_cond_wait(&_cond, _mutex.getMutexPtr());
    if(ret)
    {
        ::perror("pthread_cond_wait in Condition::wait()");
    }
}

void Condition::notify()
{
    int ret = ::pthread_cond_signal(&_cond);
    if(ret)
    {
        ::perror("pthread_cond_signal in Condition::notify()");
    }
}

void Condition::notifyAll()
{
    int ret = ::pthread_cond_broadcast(&_cond);
    if(ret)
    {
        ::perror("pthread_cond_broadcast in Condition::notifyAll()");
    }
}
